//
//  BNCalendarView.m
//  BeautifulNote
//
//  Created by Apple on 2019/7/16.
//  Copyright © 2019 Maker. All rights reserved.
//

#import "BNCalendarView.h"

#import <FSCalendar.h>
#import <EventKit/EventKit.h>

@interface BNCalendarView ()<FSCalendarDelegate, FSCalendarDataSource>

@property (nonatomic, strong) FSCalendar *calendar;
@property (nonatomic, strong) NSArray<EKEvent *> *events;

@end

@implementation BNCalendarView

#pragma mark - life cycle
- (instancetype)init {
    if (self = [super init]) {
        [self addEffect];
        [self addCalendar];
    }
    return self;
}

- (void)dealloc {
    self.delegate = nil;
}

#pragma mark - action
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if ([self.delegate respondsToSelector:@selector(didClickEmptView)]) {
        [self.delegate didClickEmptView];
    }
}

#pragma mark - FSCalendarDelegate
- (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
{
    [calendar mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(bounds.size.height));
    }];
    [self layoutIfNeeded];
}

- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition {
    if ([self.delegate respondsToSelector:@selector(didSelectedDate:)]) {
        [self.delegate didSelectedDate:date];
    }
}

#pragma mark - privateDelegate
- (void)addCalendar {
    _calendar = [[FSCalendar alloc] init];
    _calendar.dataSource = self;
    _calendar.delegate = self;
    _calendar.backgroundColor = [UIColor whiteColor];
    _calendar.layer.cornerRadius = 6;
    _calendar.layer.masksToBounds = YES;
    _calendar.locale = [NSLocale localeWithLocaleIdentifier:@"zh-CN"];
    _calendar.allowsSelection = YES;
    [self addSubview:_calendar];
    [_calendar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.width.mas_equalTo(ScreenWidth - 20);
        make.height.mas_equalTo((ScreenWidth - 20)/320 * 300);
    }];
}

- (void)addEffect {
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    //必须给effcetView的frame赋值,因为UIVisualEffectView是一个加到UIIamgeView上的子视图.
    effectView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    [self addSubview:effectView];
}

@end
